package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.sdk.funds.FundBar;
import com.qianyitian.hope2.sdk.funds.FundInfo;

import java.util.ArrayList;
import java.util.List;

import static com.qianyitian.hope2.analyzer.util.Utils.double2Percentage;

public class MA30Analyzer {

    public MA30Analyzer() {
    }

    double range;

    public boolean analyze(FundInfo fund) {
        List<Double> list = calMA30(30, 10, fund.getFundBarList());
        if (list.isEmpty()) {
            return false;
        }
        int number = 0;
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i) > list.get(i - 1)) {
                number++;
            }
        }

        double lastPrice = fund.getFundBarList().get(fund.getFundBarList().size() - 1).getValue();
        double lastMA30Price = list.get(list.size() - 1);
        range = (lastPrice / lastMA30Price - 1);
        return number >= 8 && range <= 0.042;
    }


    List<Double> calMA30(int maDays, int lastDays, List<FundBar> fundBarList) {
        List<Double> list = new ArrayList<>();
        double total = 0;

        int arraySize = fundBarList.size();

        if (arraySize < maDays + lastDays) {
            return list;
        }

        for (int i = arraySize - maDays - lastDays; i < arraySize; i += 1) {
            if (i < arraySize - lastDays) {
                total += fundBarList.get(i).getValue();
            } else {
                total = total + (fundBarList.get(i).getValue() - fundBarList.get(i - maDays).getValue());
                double kk = total / maDays;
                list.add(kk);
            }
        }
        return list;
    }

    public String getRange() {
        return double2Percentage(range);
    }

}
